package com.bootdo.contract.controller;

import java.util.List;
import java.util.Map;

import com.bootdo.contract.domain.TikuDO;
import com.bootdo.contract.service.TikuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.contract.domain.TkzhishidianDO;
import com.bootdo.contract.service.TkzhishidianService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * @author yang
 * @email 2795210596@qq.com
 * @date 2019-09-05 10:00:44
 */

@Controller
@RequestMapping ( "/contract/tkzhishidian" )
public class TkzhishidianController {
    @Autowired
    private TkzhishidianService tkzhishidianService;
    @Autowired
    private TikuService tikuService;

    @GetMapping ( "/tkzhishidian" )
    String Tkzhishidian(@RequestParam ( "id" ) Integer id, Model model) {
        TikuDO tikuDO = tikuService.get(id);
        model.addAttribute("tiku", tikuDO);
        return "contract/tkzhishidian/tkzhishidian";
    }

    @ResponseBody
    @GetMapping ( "/list" )
    public PageUtils list(@RequestParam Map <String, Object> params) {
        System.out.println(params);
        //查询列表数据
        Query query = new Query(params);
        List <TkzhishidianDO> tkzhishidianList = tkzhishidianService.list(query);
        int total = tkzhishidianService.count(query);
        PageUtils pageUtils = new PageUtils(tkzhishidianList, total);
        return pageUtils;
    }

    @GetMapping ( "/add" )
    String add(@RequestParam ( "tkid" ) Integer tkid, Model model) {
        TikuDO tikuDO = tikuService.get(tkid);
        model.addAttribute("tiku", tikuDO);
        return "contract/tkzhishidian/add";
    }

    @GetMapping ( "/edit/{id}" )
    String edit(@PathVariable ( "id" ) Integer id, Model model) {
        TkzhishidianDO tkzhishidian = tkzhishidianService.get(id);
        model.addAttribute("tkzhishidian", tkzhishidian);
        return "contract/tkzhishidian/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping ( "/save" )

    public R save(TkzhishidianDO tkzhishidian) {
        if (tkzhishidianService.save(tkzhishidian) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping ( "/update" )

    public R update(TkzhishidianDO tkzhishidian) {
        tkzhishidianService.update(tkzhishidian);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping ( "/remove" )
    @ResponseBody

    public R remove(Integer id) {
        if (tkzhishidianService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping ( "/batchRemove" )
    @ResponseBody

    public R remove(@RequestParam ( "ids[]" ) Integer[] ids) {
        tkzhishidianService.batchRemove(ids);
        return R.ok();
    }

}
